package com.wzy.Filter;

import com.wzy.Jdbc封装.pojo.News_user;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class loginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        HttpServletResponse resp = (HttpServletResponse) servletResponse;

        System.out.println("拦截");

        HttpSession session = req.getSession();
        Object obj = session.getAttribute("user");
        News_user news_user = null;
        if(obj!=null){
            news_user = (News_user)obj;
            if(news_user.getId()!=0){
                filterChain.doFilter(req,resp);
            }
        }else {
//            req.getRequestDispatcher("Login.jsp").forward(req,resp);
            resp.sendRedirect("http://localhost:8080/Login.jsp");
//            filterChain.doFilter(req,resp);
        }
        filterChain.doFilter(req,resp);

    }

    @Override
    public void destroy() {

    }
}
